package com.woniu.zsd.user.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.zsd.common.util.JWTUtil;
import com.woniu.zsd.common.util.RedisKey;
import com.woniu.zsd.common.util.Result;
import com.woniu.zsd.user.controller.form.ChatForm;
import com.woniu.zsd.user.controller.form.LoginForm;
import com.woniu.zsd.user.controller.form.SignUpForm;
import com.woniu.zsd.user.dto.UserDTO;
import com.woniu.zsd.user.param.ChatParam;
import com.woniu.zsd.user.param.LoginParam;
import com.woniu.zsd.user.param.SignUpParam;
import com.woniu.zsd.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjf
 * @since 2025年02月22日
 */
@RestController
@RequestMapping("/user")
@Validated
@Api(tags = "用户模块")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private JWTUtil jwtUtil;
    @PostMapping("/signUp")
    @ApiOperation(value = "用户注册")
    public Result signUp(@RequestBody SignUpForm form) throws Exception {
        userService.signUp(BeanUtil.toBean(form, SignUpParam.class));
        return Result.success("注册成功");
    }
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result login(@Valid @RequestBody LoginForm form, HttpServletRequest request, HttpServletResponse response, BindingResult result) throws Exception {
        System.out.println("登录参数 " + form);
        if (result.hasErrors()){
            result.getAllErrors().forEach(error -> System.out.println(error.getDefaultMessage()));
            return Result.error("参数校验失败");
        }
        UserDTO userDTO = userService.login(BeanUtil.toBean(form, LoginParam.class),request);
        Map<String, Object> map = BeanUtil.beanToMap(userDTO);
        String accessToken = jwtUtil.createToken(map, 5);
        String refreshToken = jwtUtil.createToken(map, 24*60);
        stringRedisTemplate.opsForValue().set(RedisKey.userAccessToken(accessToken), userDTO.getUserId().toString(), 5, TimeUnit.MINUTES);
        stringRedisTemplate.opsForValue().set(RedisKey.userRefreshToken(accessToken), refreshToken, 1,TimeUnit.DAYS);
        //设置token
        response.setHeader("token", accessToken);
        response.setHeader("Access-Control-Expose-Headers", "token");
        return Result.success("登录成功",userDTO);
    }
    //发送短信注册验证码
    @ApiOperation(value = "发送短信注册验证码")
    @GetMapping("/sendSignUpCode")
    public Result sendSignUpCode(@RequestParam String phone) throws Exception {
        System.out.println("发送短信验证码 " + phone);
        userService.sendSignUpCode(phone);
        return Result.success("发送成功");
    }
    //客户与客服聊天
    @PostMapping("/chat")
    @ApiOperation(value = "客户与客服聊天")
    public Result chat(@RequestBody ChatForm form){
        System.out.println("聊天内容 " + form.toString());
        userService.chat(BeanUtil.toBean(form, ChatParam.class));
        return Result.success("消息发送成功");
    }
    //获取历史聊天记录
    @ApiOperation(value = "获取历史聊天记录")
    @GetMapping("/selectChatHistory")
    public Result selectChatHistory(@RequestParam Integer userId,@RequestParam String recyclingOrderNum){
        return Result.success("查询成功",userService.selectChatHistory(userId,recyclingOrderNum));
    }

    @ApiOperation(value = "查询用户积分")
    @GetMapping("/selectPointById")
    public Result selectPointById(@RequestParam Integer userId){
        Integer point = userService.getUserPoint(userId);
        return Result.success("查询成功");
    }
    //增加用户积分
    @GetMapping("/addUserPoint")
    @ApiOperation(value = "增加用户积分")
    public Result addUserPoint(@RequestParam Integer userId,@RequestParam Integer point){
        userService.addUserPoint(userId,point);
        return Result.success("增加积分成功");
    }
    @ApiOperation(value = "tess4j验证身份证")
    //图片识别
    @PostMapping("/recognizeIdCard")
    public Result<String> recognizeIdCard(@RequestParam MultipartFile idCard) throws Exception {
        System.out.println("图片识别 ");
        System.out.println("图片识别 " + idCard);
        return Result.success("验证成功",userService.recognizeIdCard(idCard));
    }


}

